<?php

/**
 * This is the model class for table "categories".
 *
 * The followings are the available columns in table 'categories':
 * @property string $id
 * @property string $name
 * @property integer $type_id
 * @property integer $status
 * @property string $description
 * @property string $image
 * @property integer $create_time
 * @property integer $update_time
 * @property integer $deleted
 */
class Category extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Category the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'categories';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name, type_id', 'required'),
			array('type_id, status, create_time, update_time', 'numerical', 'integerOnly'=>true),
			array('deleted', 'boolean'),
			array('name', 'length', 'max'=>50),
			array('image', 'length', 'max'=>250),
			array('image', 'file', 'types'=>'jpeg,jpg,gif,png'),
			array('description', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, name, type_id, status, description, image, create_time, update_time, deleted', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
            //ten cua relation, day la con cua type, belong_to la 1 category thuoc 1 type, day la ten cua model Cha, ten thuoc tinh tham khao bang cha
            'type' => array(self::BELONGS_TO, 'Type', 'type_id'),
            'products' => array(self::HAS_MANY, 'Product', 'category_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => Yii::t('labels','ID'),
			'name' => Yii::t('category','Tên'),
			'type_id' => Yii::t('category','Loại'),
			'status' => Yii::t('category','Trạng thái'),
			'description' => Yii::t('labels','Mô tả'),
			'image' => Yii::t('category','Hình ảnh'),
			'create_time' => Yii::t('labels','Ngày tạo'),
			'update_time' => Yii::t('labels','Ngày sửa'),
			'deleted' => Yii::t('labels','Xóa'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('type_id',$this->type_id);
		$criteria->compare('status',$this->status);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('image',$this->image,true);
		$criteria->compare('create_time',$this->create_time);
		$criteria->compare('update_time',$this->update_time);
		$criteria->compare('deleted',$this->deleted);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
    
    public function behaviors(){
		return array(
			'CTimestampBehavior' => array(
				'class' => 'zii.behaviors.CTimestampBehavior',
			)
		);
	}
}